/* ========================================================================== */
/*   GameTime.cpp                                                             */
/*   (c) 2012 Team 7 Hogeschool Utrecht ICT TI								  */
/*		Lars Stolwijk, Gerben Boot, Guido van Hienen, Marcel Boelen			  */
/*                                                                            */
/*   Tracking and sharing gametime (in seconds), gives signal when the		  */
/*		playtime is identical to the gametime								  */
/* ========================================================================== */

#include "GameTime.h"

void GameTime::RegisterGameHandler(GameHandler * gameHandler)
{
	theGameHandler = gameHandler;
}

void GameTime::main()
{
	CurrentState = WAITING;
	gameTime = _GAMETIME;
	while(true)
	{
		if(CurrentState == WAITING)
		{
			//std::cout << "GT->WAITING\n";
			theProcessHitController->WriteGameTime((-1));
			RTOS::event e = wait( StartStopGameTimeFlag );
			CurrentState = RUNNING;
		}
		else if(CurrentState == RUNNING)
		{
			//std::cout << "GT->RUNNING\n";
			time = 0;
			theProcessHitController->WriteGameTime(time);
			while(time < gameTime)
			{
				RTOS::event e = wait( );
				if(e == GTTClock)
				{
					time++;
					theProcessHitController->WriteGameTime(time);					
				}
				else if(e == StartStopGameTimeFlag)
				{
					CurrentState = WAITING;
					break;
				}
			}
			theGameHandler->ZeroGameTime();
			CurrentState = WAITING;
		}
	}
}

void GameTime::StartStopGameTime()
{
	StartStopGameTimeFlag.set();
}
